//
//  ShoppingCollectionViewCell.m
//  Project_A Gary
//
//  Created by leilei on 16/3/28.
//  Copyright © 2016年 www.lei.com. All rights reserved.
//

#import "ShoppingCollectionViewCell.h"
#import "EvaluateTableViewCell.h"
#import "EvaluateModel.h"
#import "DetailViewController.h"


@interface ShoppingCollectionViewCell ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain)UITableView *tableV;
@property (nonatomic, retain)NSMutableArray *dataArr;
@end


@implementation ShoppingCollectionViewCell

-(void)dealloc
{
    [super dealloc];
    [_tableV release];
    [_dataArr release];
    [_navi release];

}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
    }
    return self;
}

- (void)createTableView
{
    self.tableV = [[UITableView alloc] initWithFrame: CGRectMake(0, 0, BOUNDS_WIDTH, BOUNDS_HEIGHT - 64 - 49 - 40) style:UITableViewStylePlain];
    self.tableV.backgroundColor = [UIColor whiteColor];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.rowHeight = 80;
    [self.contentView addSubview:self.tableV];
    
    [self.tableV registerClass:[EvaluateTableViewCell class] forCellReuseIdentifier:@"EvaluateTableViewCell"];
    [_tableV release];
    
}



- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    [self dataHandle];
}


- (void)dataHandle
{
    NSString *str = @"http://mrobot.pcauto.com.cn/v2/cms/channels/3?pageNo=1&pageSize=20&v=4.0.0";
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        self.dataArr = [EvaluateModel transformWithArray:responseObject[@"data"]];
        [_tableV reloadData];
        
    } filed:^(NSError *error) {
        
    }];
    
}

#pragma mark - 点击进入详情页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detail = [[DetailViewController alloc] init];
    EvaluateModel *eModel = self.dataArr[indexPath.row];
    
    detail.url = eModel.url;
    [self.navi pushViewController:detail animated:YES];
    
}

// 两个tableView的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EvaluateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EvaluateTableViewCell" forIndexPath:indexPath];
    EvaluateModel *em = self.dataArr[indexPath.row];
    //标题/ 评论数
    cell.titleLab.text = em.title;
    cell.timeLab.text = em.pubDate;
    // 评论数
    if ([em.count isEqualToNumber:@(0)]) {
        cell.countLab.text = @"抢沙发";
    } else{
        cell.countLab.text = [NSString stringWithFormat:@"评论数%@", em.count];
    }
    // 图片
    [cell.leftImgView sd_setImageWithURL:[NSURL URLWithString:em.image]];
    
    return cell;
}



@end
